﻿using SharpErp.Data.Common.Entity;
using SharpErp.Data.Common.View;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SharpErp.Data.Service
{
    public partial class SharpErpServiceImpl : ISharpErpService
    {
        public List<MaterialFirstClass> GetSysMaterialFirstClass()
        {
            var list = GetAllKvMaterialClass<MaterialFirstClass>(KVEntity.KEY_MATERIAL_FIRST_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public void AddMaterialFirstClass(MaterialFirstClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialFirstClass>(KVEntity.KEY_MATERIAL_FIRST_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIRST_CLASS);
        }



        public void UpdateMaterialFirstClass(MaterialFirstClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialFirstClass>(KVEntity.KEY_MATERIAL_FIRST_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    Materials[i].Code = Material.Code;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIRST_CLASS);
            }
        }

        public void DeleteMaterialFirstClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialFirstClass>(KVEntity.KEY_MATERIAL_FIRST_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIRST_CLASS);
            }
        }

        public string GetMaterialFirstClassCode(int id)
        {
            string maxCode = "0000";
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialFirstClass>(KVEntity.KEY_MATERIAL_FIRST_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        return Materials[i].Code;
                    }
                    if (Materials[i].Code.CompareTo(maxCode) > 0) maxCode = Materials[i].Code;
                }
            }
            return string.Format("{0:D4}", int.Parse(maxCode) + 1);
        }

        public List<MaterialSecondClass> GetSysMaterialSecondClass()
        {
            var list = GetAllKvMaterialClass<MaterialSecondClass>(KVEntity.KEY_MATERIAL_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0).ToList();
        }

        public List<MaterialSecondClass> GetSysMaterialSecondClassByParentId(int parentId)
        {
            var list = GetSysMaterialSecondClass();
            if (list == null || !list.Any()) return list;
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
            ;
        }

        public void AddMaterialSecondClass(MaterialSecondClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialSecondClass>(KVEntity.KEY_MATERIAL_SECOND_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SECOND_CLASS);
        }

        public void UpdateMaterialSecondClass(MaterialSecondClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialSecondClass>(KVEntity.KEY_MATERIAL_SECOND_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    Materials[i].ManufactorName = Material.ManufactorName;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SECOND_CLASS);
            }
        }

        public void DeleteMaterialSecondClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialSecondClass>(KVEntity.KEY_MATERIAL_SECOND_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SECOND_CLASS);
            }
        }

        public List<MaterialThirdClass> GetSysMaterialThirdClassByParentId(int parentId)
        {
            var list = GetAllKvMaterialClass<MaterialThirdClass>(KVEntity.KEY_MATERIAL_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddMaterialThirdClass(MaterialThirdClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialThirdClass>(KVEntity.KEY_MATERIAL_THIRD_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_THIRD_CLASS);
        }

        public void UpdateMaterialThirdClass(MaterialThirdClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialThirdClass>(KVEntity.KEY_MATERIAL_THIRD_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    Materials[i].Deduct = Material.Deduct;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_THIRD_CLASS);
            };
        }

        public void DeleteMaterialThirdClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialThirdClass>(KVEntity.KEY_MATERIAL_THIRD_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_THIRD_CLASS);
            }
        }

        public List<MaterialForthClass> GetSysMaterialFourClassByParentId(int parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialForthClass>(KVEntity.KEY_MATERIAL_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddMaterialFourClass(MaterialForthClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialForthClass>(KVEntity.KEY_MATERIAL_FORTH_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FORTH_CLASS);
        }

        public void UpdateMaterialFourClass(MaterialForthClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialForthClass>(KVEntity.KEY_MATERIAL_FORTH_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FORTH_CLASS);
            };
        }

        public void DeleteMaterialFurClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialForthClass>(KVEntity.KEY_MATERIAL_FORTH_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FORTH_CLASS);
            }
        }

        public List<MaterialFifthClass> GetSysMaterialFiveClassByParentId(int parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialFifthClass>(KVEntity.KEY_MATERIAL_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddMaterialFiveClass(MaterialFifthClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialFifthClass>(KVEntity.KEY_MATERIAL_FIFTH_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIFTH_CLASS);
        }

        public void UpdateMaterialFiveClass(MaterialFifthClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialFifthClass>(KVEntity.KEY_MATERIAL_FIFTH_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIFTH_CLASS);
            };
        }

        public void DeleteMaterialFiveClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialFifthClass>(KVEntity.KEY_MATERIAL_FIFTH_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_FIFTH_CLASS);
            }
        }

        public List<MaterialSixClass> GetSysMaterialSixClassByParentId(int parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialSixClass>(KVEntity.KEY_MATERIAL_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 & p.ParentId == parentId).ToList();
        }

        public void AddMaterialSixClass(MaterialSixClass Material)
        {
            var Materials = GetAllKvMaterialClass<MaterialSixClass>(KVEntity.KEY_MATERIAL_SIXTH_CLASS);
            var maxId = 1;
            if (Materials != null && Materials.Any()) maxId = (from p in Materials select p.Id).Max() + 1;
            Material.Id = maxId;
            Material.CreateTime = Material.LastModifyTime = new DateTime();
            if (Materials == null) return;
            Materials.Add(Material);
            SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SIXTH_CLASS);
        }

        public void UpdateMaterialSixClass(MaterialSixClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialSixClass>(KVEntity.KEY_MATERIAL_SIXTH_CLASS);
                for (var i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id != Material.Id) continue;
                    Materials[i].LastModifyTime = new DateTime();
                    Materials[i].Name = Material.Name;
                    break;
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SIXTH_CLASS);
            };
        }

        public void DeleteMaterialSixClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = GetAllKvMaterialClass<MaterialSixClass>(KVEntity.KEY_MATERIAL_SIXTH_CLASS);
                for (int i = 0; i < Materials.Count(); ++i)
                {
                    if (Materials[i].Id == id)
                    {
                        Materials[i].LastModifyTime = new DateTime();
                        Materials[i].SystemFlag |= BaseEntity.SYS_FLAG_DELETE;
                        break;
                    }
                }
                SetAllKvMaterialClass(Materials, KVEntity.KEY_MATERIAL_SIXTH_CLASS);
            }
        }

        public List<MaterialSevenClass> GetSysMaterialSevenClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<SharpErp.Data.Common.Entity.MaterialSevenClass>(context.AllArray(context.MaterialSevenClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId));
            }
        }


        public void AddMaterialSevenClass(MaterialSevenClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Add(context.MaterialSevenClasses, Material);
            }
        }

        public void UpdateMaterialSevenClass(MaterialSevenClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Update(context.MaterialSevenClasses, Material);
            }
        }

        public void DeleteMaterialSevenClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Delete(context.MaterialSevenClasses, p => p.Id == id);
            }
        }


        public List<MaterialEightClass> GetSysMaterialEightClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<SharpErp.Data.Common.Entity.MaterialEightClass>(context.AllArray(context.MaterialEightClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId));
            }
        }

        public void AddMaterialEightClass(MaterialEightClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Add(context.MaterialEightClasses, Material);
            }
        }

        public void UpdateMaterialEightClass(MaterialEightClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Update(context.MaterialEightClasses, Material);
            }
        }

        public void DeleteMaterialEightClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Delete(context.MaterialEightClasses, p => p.Id == id);
            }
        }

        public List<MaterialNineClass> GetSysMaterialNineClassByParentId(int parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new List<MaterialNineClass>(context.AllArray(context.MaterialNineClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && p.ParentId == parentId));
            }
        }

        public void AddMaterialNineClass(MaterialNineClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Add(context.MaterialNineClasses, Material);
            }
        }

        public void UpdateMaterialNineClass(MaterialNineClass Material)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Update(context.MaterialNineClasses, Material);
            }
        }


        public void DeleteMaterialNineClass(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                context.Delete(context.MaterialNineClasses, p => p.Id == id);
            }
        }
        public List<MaterialSecondClass> GetSysMaterialSecondClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvMaterialClass<MaterialSecondClass>(KVEntity.KEY_MATERIAL_SECOND_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<MaterialThirdClass> GetSysMaterialThirdClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvMaterialClass<MaterialThirdClass>(KVEntity.KEY_MATERIAL_THIRD_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<MaterialForthClass> GetSysMaterialFourClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialForthClass>(KVEntity.KEY_MATERIAL_FORTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<MaterialFifthClass> GetSysMaterialFiveClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialFifthClass>(KVEntity.KEY_MATERIAL_FIFTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<MaterialSixClass> GetSysMaterialSixClassByParentIds(HashSet<int> parentId)
        {
            var list = GetAllKvMaterialClass<SharpErp.Data.Common.Entity.MaterialSixClass>(KVEntity.KEY_MATERIAL_SIXTH_CLASS);
            if (list == null || !list.Any())
            {
                return list;
            }
            return list.Where(p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)).ToList(); ;
        }

        public List<MaterialSevenClass> GetSysMaterialSevenClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<SharpErp.Data.Common.Entity.MaterialSevenClass>(context.AllArray(context.MaterialSevenClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)));
            };
        }

        public List<MaterialEightClass> GetSysMaterialEightClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<SharpErp.Data.Common.Entity.MaterialEightClass>(context.AllArray(context.MaterialEightClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)));
            };
        }

        public List<MaterialNineClass> GetSysMaterialNineClassByParentIds(HashSet<int> parentId)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                return new System.Collections.Generic.List<SharpErp.Data.Common.Entity.MaterialNineClass>(context.AllArray(context.MaterialNineClasses).Where(
                    p => (p.SystemFlag & BaseEntity.SYS_FLAG_DELETE) == 0 && parentId.Contains(p.ParentId)));
            };
        }

        public string GetMaterialEightClassCode(int id)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                if (id > 0)
                {
                    var Material = context.SelectOne(context.MaterialEightClasses, p => p.Id == id);
                    if (Material != null)
                    {
                        return Material.Code;
                    }
                }

                var maxId = context.MaterialEightClasses.Max(p => p.Code != null ? p.Code.Substring(p.Code.Length - 7) : p.Code); ;
                if (string.IsNullOrEmpty(maxId))
                {
                    return "S0000001";
                }
                else
                {
                    var number = maxId.Substring(maxId.Length - 7);
                    return string.Format("S{0:D7}", int.Parse(number) + 1);
                }

            }
        }

        public void CloneMaterialSevenClass(HashSet<int> ids, bool includeChildren)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var sevenClassList = context.SelectAll(context.MaterialEightClasses, p => ids.Contains(p.Id));
                foreach (var MaterialSevenClass in sevenClassList)
                {
                    var Id = MaterialSevenClass.Id;
                    MaterialSevenClass.Id = 0;
                    var newMaterialSevenClass = context.Add(context.MaterialEightClasses, MaterialSevenClass);
                    if (includeChildren)
                    {
                        var eightClassList = context.SelectAll(context.MaterialNineClasses, p => p.ParentId == Id);
                        foreach (var MaterialEightClass in eightClassList)
                        {
                            MaterialEightClass.Id = 0;
                            MaterialEightClass.ParentId = newMaterialSevenClass.Id;
                            context.Add(context.MaterialNineClasses, MaterialEightClass);
                        }
                    }
                    context.SaveChanges();
                }
            }
        }

        public void CloneMaterialEightClass(HashSet<int> ids)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var eightClassList = context.SelectAll(context.MaterialNineClasses, p => ids.Contains(p.Id));
                foreach (var MaterialEightClass in eightClassList)
                {
                    MaterialEightClass.Id = 0;
                    context.Add(context.MaterialNineClasses, MaterialEightClass);
                }
                context.SaveChanges();

            }
        }

        private QueryGoodsInfoView getQueryMaterialInfoView(int nineClassId)
        {

            using (var context = new Common.Context.SqliteDbContext())
            {
                var nineClassItem = context.SelectOne(context.MaterialNineClasses, p => p.Id == nineClassId);
                var eightClassItem = context.SelectOne(context.MaterialEightClasses, p => p.Id == nineClassItem.ParentId);
                return getQueryGoodsInfoView(eightClassItem, nineClassItem);
            }

        }

        private QueryGoodsInfoView getQueryGoodsInfoView(MaterialEightClass eightClass, MaterialNineClass nineClass)
        {
            return new QueryGoodsInfoView() {
                            Name = eightClass.Name,
                            Code = eightClass.Code,
                            Standard = eightClass.Standard,
                            Length = nineClass.Length,
                            Facility = eightClass.Facility,
                            Unit = eightClass.Unit,
                            ParentDesc = eightClass.ParentDesc,
                            Area = nineClass.Area,
                            BuyPrice = nineClass.BuyPrice,
                            Inventory = eightClass.Inventory,
                            Pcount = eightClass.Pcount,
                            Width = nineClass.Width,
                            ParentId = eightClass.ParentId,
                            EightClassId = eightClass.Id,
                            NineClassId = nineClass.Id,
            };
        }

        public List<QueryGoodsInfoView> SearchMaterialInfo(String keyWords)
        {
            using (var context = new Common.Context.SqliteDbContext())
            {
                var Materials = keyWords.Split(new char[] { ' ', }, StringSplitOptions.RemoveEmptyEntries);
                var queryResult = new List<QueryGoodsInfoView>();
                List< MaterialEightClass > sevenClassList = null;
                foreach(var Material in Materials)
                {
                    if(sevenClassList == null || sevenClassList.Count == 0)
                    {
                        sevenClassList = new List<MaterialEightClass>(context.SelectAll(context.MaterialEightClasses, p => p.Name != null && p.Name.Contains(Material) ||
                        p.Code != null && p.Code.Contains(Material) ||
                        p.ParentDesc != null && p.ParentDesc.Contains(Material)));
                    }
                    else
                    {
                        sevenClassList = sevenClassList.Where(p => p.Name != null && p.Name.Contains(Material) ||
                        p.Code != null && p.Code.Contains(Material) ||
                        p.ParentDesc != null && p.ParentDesc.Contains(Material)).ToList();
                    }
                }
                foreach(var Material in sevenClassList)
                {
                    var eightClassList = this.GetSysMaterialNineClassByParentId(Material.Id);
                    foreach(var eightClassMaterial in eightClassList)
                    {
                        queryResult.Add(getQueryGoodsInfoView(Material, eightClassMaterial));
                    }
                }
                return queryResult;
            }
        }
    }

}
